<template>
  <div id="userLoginView">
    <a-form
      style="max-width: 480px; margin: 0 auto"
      auto-label-width
      label-align="left"
      :model="form"
      :style="{ width: '600px' }"
      @submit="handleSubmit"
    >
      <a-form-item field="userAccount" label="Username">
        <a-input v-model="form.userAccount" placeholder="Enter Your Username" />
      </a-form-item>
      <a-form-item
        field="userPassword"
        tooltip="密码不少于8位"
        label="Password"
      >
        <a-input-password
          v-model="form.userPassword"
          placeholder="Enter Your Password"
        />
      </a-form-item>
      <a-form-item
        field="checkPassword"
        tooltip="密码不少于8位"
        label="RepeatPW"
      >
        <a-input-password
          v-model="form.checkPassword"
          placeholder="Enter Your Password Again"
        />
      </a-form-item>
      <a-form-item>
        <a-button type="primary" html-type="submit">Submit</a-button>
        <a-button
          type="primary"
          style="width: 70px; margin-left: 230px"
          html-type="register"
          @click="returnLogin"
          >Return</a-button
        >
      </a-form-item>
    </a-form>
  </div>
</template>
<script setup lang="ts">
import { Message } from "@arco-design/web-vue";
import {
  UserControllerService,
  UserLoginRequest,
  UserRegisterRequest,
} from "../../generated";
import { reactive } from "vue";
import { useRouter } from "vue-router";
import { useStore } from "vuex";

const form = reactive({
  userAccount: "",
  userPassword: "",
  checkPassword: "",
} as UserRegisterRequest);

const router = useRouter();
const store = useStore();

const returnLogin = () => {
  router.push({
    path: "/user/login",
    replace: true,
  });
};

const handleSubmit = async () => {
  const res = await UserControllerService.userRegisterUsingPost(form);
  if (res.code === 0) {
    await store.dispatch("user/getLoginUser");
    Message.success("注册成功");
    router.push({
      path: "/user/login",
      replace: true,
    });
  } else {
    Message.error("注册失败！" + res.message);
  }
};
</script>
